/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: Lenovo
 * Date: 2025-07-16
 * Time: 22:51
 */
//https://leetcode.cn/problems/construct-string-from-binary-tree/submissions/644586784/
// 给你二叉树的根节点 root ，请你采用前序遍历的方式，将二叉树转化为一个由括号和整数组成的字符串，返回构造出的字符串。
//空节点使用一对空括号对 "()" 表示，转化后需要省略所有不影响字符串与原始二叉树之间的一对一映射关系的空括号对。
class TreeNode {
    int val;
    TreeNode left;
    TreeNode right;
    TreeNode() {}
    TreeNode(int val) { this.val = val; }
    TreeNode(int val, TreeNode left, TreeNode right) {
        this.val = val;
        this.left = left;
        this.right = right;
    }
}
public class Test {
    static StringBuilder str = new StringBuilder();
    public static String tree2str(TreeNode root) {
        str.append('[');
        fun(root);
        str.append(']');
        return str.toString();
    }

    public static void fun(TreeNode root) {
        str.append(root.val);
        if(root.left!=null){
            str.append('(');
            fun(root.left);
            str.append(')');
        }
        else if(root.right!=null){
            str.append('(');
            str.append(')');
        }
        if (root.right != null) {
            str.append('(');
            fun(root.right);
            str.append(')');
        }
    }
    public static void main(String[] args) {
        TreeNode A1=new TreeNode(1);
        TreeNode A2=new TreeNode(2);
        TreeNode A3=new TreeNode(3);
        TreeNode A4=new TreeNode(4);
        TreeNode A5=new TreeNode(5);
        TreeNode A6=new TreeNode(6);
        TreeNode A7=new TreeNode(7);
        A1.left=A2;A1.right=A3;
        A2.left=A4;A2.right=A5;
        A3.left=A6;A3.right=A7;
        String str=tree2str(A1);
        System.out.println(str);
    }
}
